using System;
using System.Collections.Generic;
using System.Text;

namespace CoreCalc.SheetDefinedFunctions
{
  public class SheetCreation
  {
    public static void AddSDF(string methodName, string[] inputCellsInA1Format, string sheetName, Workbook wb,
        string outputCellInA1Format, Version markVersion)
    {
      AddSDFGeneric(methodName, inputCellsInA1Format, wb[sheetName], wb,
          outputCellInA1Format, markVersion);
    }

    public static void AddSDF(string methodName, string[] inputCellsInA1Format, int sheetIndex, Workbook wb,
        string outputCellInA1Format, Version markVersion)
    {
      AddSDFGeneric(methodName, inputCellsInA1Format, wb[sheetIndex], wb,
          outputCellInA1Format, markVersion);
    }

    private static void AddSDFGeneric(string methodName, string[] inputCellsInA1Format, Sheet sheet, Workbook wb,
        string outputCellInA1Format, Version markVersion)
    {
      if (wb.SheetCount == 0)
        return;

      List<FullCellAddr> inputAddresses = new List<FullCellAddr>();

      foreach (string s in inputCellsInA1Format)
      {
        inputAddresses.Add(new FullCellAddr(s, sheet));
      }

      FullCellAddr outputCell = new FullCellAddr(outputCellInA1Format, sheet);
      SdfManager.MarkVersion = markVersion;
      SdfManager.DeleteMethod(methodName);
      SdfManager.CreateMethod(methodName, outputCell, inputAddresses);
    }
  }
}
